home *** CD-ROM | disk | FTP | other *** search
- Path: newserv.agcs.com!news
- From: "George B. T. Greene" <greeneg@agcs.com>
- Newsgroups: comp.lang.c
- Subject: Re: Rand() Function
- Date: 22 Mar 1996 14:21:11 GMT
- Organization: AG Communication Systems, ITS
- Message-ID: <4iud0n$dks@newserv.agcs.com>
- References: <4iqltj$40g@news1.sympatico.ca>
- NNTP-Posting-Host: greeneg.agcs.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.2N (Windows; I; 32bit)
-
- Gisele Swinson <gisele.swinson@sympatico.ca> wrote:
- >How do I get a random number from a "SET" of numbers.
-
- .. depends a bit on the set :-)
-
- >e.g.
- > I want a random number from this set:
- >
- > 2, 4, 6, 8, 10
-
- OK, for a small set like this, assuming rand( ) is good enough for your
- purposes, you could try (this approach will work for _any_ "small" set):
-
- #define NUMELEMENTS 5
- int set[NUMELEMENTS] = {2, 4, 6, 8, 10};
- int n;
- ..
- n = set[rand( ) % NUMELEMENTS];
-
- or for that particular case, more simply:
-
- n = (rand( ) % 5 + 1) * 2;
-
-